package com.mingyizhudao.app.server;

import com.mingyizhudao.app.http.DataUtils;
import com.mingyizhudao.app.utils.Constants;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;

public class DataCheckService extends Service{
	private Context mContext;
	private int sum = 0;
	private Handler handler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if(msg.what == Constants.Result_Status.RESULT_SCCESS){
				Log.e("===", "data download success");
				stopSelf();
			}else if(msg.what == Constants.Result_Status.RESULT_FAILURE){
				Log.e("===", "data download FAILURE");
				sum++;
				if (sum <= 5) getLocalData();
				else stopSelf();
			}
		}
		
	};
	@Override
	public void onCreate() {
		super.onCreate();
		mContext = this;
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		Log.e("===", "server onDestroy");
	}

	@Override
	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);
		getLocalData();
		Log.e("===", "server start");
	}

	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}
	
	private void getLocalData(){
		DataUtils.getInitial().getCheckData(mContext, null,handler);
	}

}
